﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.Logging;
using Webapiv3.Domain.Entity;
using Webapiv3.Domain.Enum;
using Webapiv3.inetrface;

namespace Webapiv3.Implement.Log
{
    public class Log : ILog
    {
        private readonly IRespository<Logs> _LogsRespository;
        public Log(IRespository<Logs> LogsRespository)
        {
            _LogsRespository = LogsRespository;
        }
        public void Clear()
        {
            var list = _LogsRespository.Table.ToList();
            _LogsRespository.Delete(list);
        }

        public void Delete(Logs Log)
        {
            _LogsRespository.Delete(Log);
            
        }

        public Logs GetLogById(int Id)
        {
            return _LogsRespository.GetById(Id);
        }

        public void Insert(Logs Log)
        {
           _LogsRespository.Insert(Log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, LogsLevel logLevel = LogsLevel.错误, int userId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;
            var list = _LogsRespository.Table.Where(x => x.CreateTime >= bTime && x.CreateTime <= eTime && x.LogLevel == (int)logLevel);
            if (userId != 0)
            {
                list = list.Where(x => x.UserId == userId);

            }
            return list;
        }

        
    }
}
